﻿Imports System.Data
Imports System.Data.SqlClient
Public Class Form7

    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Dim SQL As String = "select usuario from Usuarios WHERE Usuario='" & TextBox1.Text & "'"
            Dim da As SqlDataAdapter
            Dim dt As New DataTable
            Try
                da = New SqlDataAdapter(SQL, nCnn)
                da.Fill(dt)
                If dt.Rows.Count <> 0 Then
                    MsgBox("El usuario ya existe", MsgBoxStyle.Critical)
                    TextBox1.Text = ""
                    TextBox1.Focus()
                Else
                    TextBox2.Focus()
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.Lowercase)
        TextBox1.SelectionStart = Len(TextBox1.Text)
    End Sub

    Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
        If Asc(e.KeyChar) = 13 Then
            TextBox3.Focus()
        End If
    End Sub

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
        If Asc(e.KeyChar) = 13 Then
            If TextBox2.Text = TextBox3.Text Then
                TextBox4.Focus()
            Else
                MsgBox("El password no coincide", MsgBoxStyle.Critical)
                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox2.Focus()
            End If
        End If
    End Sub

    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged

    End Sub

    Private Sub TextBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox4.KeyPress
        If Asc(e.KeyChar) = 13 Then
            TextBox5.Focus()
        End If
    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
        TextBox4.Text = StrConv(TextBox4.Text, VbStrConv.Uppercase)
        TextBox4.SelectionStart = Len(TextBox4.Text)
    End Sub

    Private Sub TextBox5_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox5.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Button1.Enabled = True
            Button1.Focus()
        End If
    End Sub

    Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
        TextBox5.Text = StrConv(TextBox5.Text, VbStrConv.Uppercase)
        TextBox5.SelectionStart = Len(TextBox5.Text)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim resp = MsgBox("¿ Desea registrar el usuario ?", MsgBoxStyle.YesNo + MsgBoxStyle.Question)
        If resp = vbYes Then
            Dim SQL As String = "INSERT INTO Usuario Values('" & TextBox1.Text & "', '" & TextBox3.Text & "', '" & TextBox4.Text & "', '" & TextBox5.Text & "'" & ")"
            Conn.Open()
            Try
                Dim cdo As New SqlCommand(SQL, Conn)
                cdo.ExecuteNonQuery()

                MsgBox("Registro exitoso", MsgBoxStyle.Information)
                Button1.Enabled = False
                TextBox1.Text = ""
                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox4.Text = ""
                TextBox5.Text = ""
                TextBox1.Focus()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
    End Sub
End Class